package com.situ.allaia.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.allaia.dao.GoodDao;
import com.situ.allaia.model.Good;
import com.situ.allaia.model.GoodSearchBean;
import com.situ.allaia.service.GoodService;
import com.situ.allaia.util.PaginateInfo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;


@Service
public class GoodServiceImp implements GoodService {

    private final GoodDao goodDao;

    public GoodServiceImp(GoodDao goodDao) {
        this.goodDao = goodDao;
    }

    @Override
    public List<Good> findAll(GoodSearchBean gsb, PaginateInfo pi) {
        PageHelper.startPage(pi.getPageNo(), pi.getPageSize());

        List<Good> goods = goodDao.findAll(gsb);

        PageInfo<Good> page = new PageInfo<>(goods);
        pi.setTotal(page.getTotal());

        return goods;
    }

    @Override
    public Good findById(Integer id) {
        return goodDao.findById(id);
    }

    @Override
    public boolean patch(Good good) {
        return goodDao.updateByPrimaryKeySelective(good) > 0;
    }

    @Override
    public boolean save(Good good) {
        good.setGmtCreate(LocalDateTime.now());
        good.setGmtUpdate(LocalDateTime.now());
        return goodDao.insert(good) > 0;
    }

    @Override
    public boolean update(Good good) {
        good.setGmtUpdate(LocalDateTime.now());
        return goodDao.updateByPrimaryKeySelective(good) > 0;
    }

    @Override
    public int deleteByIds(Collection<Integer> ids) {
        return goodDao.deleteByIds(ids);
    }
}
